import {createRouter, createWebHashHistory} from 'vue-router'
import Home from '../views/Home.vue'
import Login from "@/views/Login";
import Test from "@/views/Test";
import Welcome from "@/components/Welcome";
import Users from "@/components/user/Users";
import Roles from "@/components/role/Roles";
import Campus from "@/components/campus/Campus";
import Rights from "@/components/rights/Rights";
import AssetBuy from "@/components/asset/AssetBuy";
import Assets from "@/components/asset/Assets";
import Approves from "@/components/asset/Approvals";
import GenerateReport from "@/components/report/GenerateReport";
import Log from "@/components/log/Logs";


const routes = [
    {path: "/login", component: Login},
    {path: "/", redirect: "/login"},
    {
        path: "/home", component: Home, redirect: '/welcome',
        children: [
            {path: '/welcome', component: Welcome},
            {path: '/users', component: Users},
            {path: '/roles', component: Roles},
            {path: '/campus', component: Campus},
            {path: '/rights', component: Rights},
            {path: '/buy', component: AssetBuy},
            {path: '/assets', component: Assets},
            {path: '/approves', component: Approves},
            {path: '/genReport', component: GenerateReport},
            {path: '/log', component: Log},
        ]
    },
    {path: "/test", component: Test}
]


// 创建路由
const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

// 全局守卫：登录拦截 本地没有存token,请重新登录
router.beforeEach((to, from, next) => {
    const token = sessionStorage.getItem("token");
    if (token) {
        if (to.path === '/login') {
            next('/home');
        } else {
            next();
        }
    } else {
        if (to.path === '/login') next()
        else next('/login')
    }

});


// 导出路由
export default router